#!/bin/bash
# ctl_sap_server.sh - version 1.1
#
# Bash script used to specify all the environment variables.
#
# Notes:
# - This has been created from the original Sapphire scripts.
#
# Change logs:
#	1.0: (hicham.x.medkouri@jpmorgan.com), intial version
#	1.1: (hicham.x.medkouri@jpmorgan.com), implemented a the stop
#		 function

source env_sap_server.sh

INSTANCE_PREFIX=SAP_RISK

# For Cygwin, switch paths to Windows format before running java
if ${cygwin}; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
  [ -n "$JAVA_OPTS" ] && JAVA_OPTS=`cygpath --path --windows "$JAVA_OPTS"`
  [ -n "$ALIB_KEY" ] && ALIB_KEY=`cygpath --absolute --windows "$ALIB_KEY"`
  [ -n "$BASE_DIR" ] && BASE_DIR=`cygpath --absolute --windows "$BASE_DIR"`
  [ -n "$BLUE_USER_DIR" ] && BLUE_USER_DIR=`cygpath --absolute --windows "$BLUE_USER_DIR"`
  [ -n "$CBB_CLIENT_LIB" ] && CBB_CLIENT_LIB=`cygpath --absolute --windows "$CBB_CLIENT_LIB"`
  [ -n "$EGO_CONFDIR" ] && EGO_CONFDIR=`cygpath --absolute --windows "$EGO_CONFDIR"`
  [ -n "$LIBDIR" ] && LIBDIR=`cygpath --absolute --windows "$LIBDIR"`
  [ -n "$DLLSDIR" ] && DLLSDIR=`cygpath --absolute --windows "$DLLSDIR"`
  [ -n "$SAPPHIRE_HOME" ] && SAPPHIRE_HOME=`cygpath --absolute --windows "$SAPPHIRE_HOME"`
  [ -n "$SAPPHIRE_APP_DIR" ] && SAPPHIRE_APP_DIR=`cygpath --absolute --windows "$SAPPHIRE_APP_DIR"`
  [ -n "$SAPPHIRE_CONFIG_DIR" ] && SAPPHIRE_CONFIG_DIR=`cygpath --absolute --windows "$SAPPHIRE_CONFIG_DIR"`
  [ -n "$SAPPHIRE_DATA_DIR" ] && SAPPHIRE_DATA_DIR=`cygpath --absolute --windows "$SAPPHIRE_DATA_DIR"`
  [ -n "$SAPPHIRE_LOC" ] && SAPPHIRE_LOC=`cygpath --absolute --windows "$SAPPHIRE_LOC"`
  [ -n "$SAPPHIRE_LOG_DIR" ] && SAPPHIRE_LOG_DIR=`cygpath --absolute --windows "$SAPPHIRE_LOG_DIR"`
  [ -n "$SAPPHIRE_RUNNING_SERVERS_FILE" ] && SAPPHIRE_RUNNING_SERVERS_FILE=`cygpath --absolute --windows "$SAPPHIRE_RUNNING_SERVERS_FILE"`
  [ -n "$SAPPHIRE_RUNTIME_DIR" ] && SAPPHIRE_RUNTIME_DIR=`cygpath --absolute --windows "$SAPPHIRE_RUNTIME_DIR"`
  [ -n "$BLUE_VERSION_PATH" ] && BLUE_VERSION_PATH=`cygpath --absolute --windows "$BLUE_VERSION_PATH"`
  [ -n "$SAPPHIRE_SCRIPTS_LOC" ] && SAPPHIRE_SCRIPTS_LOC=`cygpath --absolute --windows "$SAPPHIRE_SCRIPTS_LOC"`
  [ -n "$SAPPHIRE_GEMFIRE_PROP_FILE" ] && SAPPHIRE_GEMFIRE_PROP_FILE=`cygpath --absolute --windows "$SAPPHIRE_GEMFIRE_PROP_FILE"`
  [ -n "$SAPPHIRE_GEMFIRE_CACHE_FILE" ] && SAPPHIRE_GEMFIRE_CACHE_FILE=`cygpath --absolute --windows "$SAPPHIRE_GEMFIRE_CACHE_FILE"` 
  [ -n "$LOG4J_LOC" ] && LOG4J_LOC=`cygpath --absolute --windows "$LOG4J_LOC"`
  [ -n "$SAP_LOG4J_LOC_PREFIX" ] && SAP_LOG4J_LOC_PREFIX=`cygpath --absolute --windows "$SAP_LOG4J_LOC_PREFIX"`
  [ -n "$LD_LIBRARY_PATH" ] && LD_LIBRARY_PATH=`cygpath --path --windows "$LD_LIBRARY_PATH"`
  [ -n "$SB_HOME" ] && SB_HOME=`cygpath --absolute --windows "$SB_HOME"`
  [ -n "$SOAM_HOME" ] && SOAM_HOME=`cygpath --absolute --windows "$SOAM_HOME"`
  [ -n "$SOAM_LIB" ] && SOAM_LIB=`cygpath --absolute --windows "$SOAM_LIB"`
  export PATH="$PATH;$LD_LIBRARY_PATH"
fi

jmx() {
	_log
	_log "setting JMX_OPTS variables"
	export JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote"
	export JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.port=${JMX_PORT}"
	export JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
	export JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
	export JAVA_OPTS="${JAVA_OPTS} ${JMX_OPTS}"
	_log JAVA_OPTS=${JAVA_OPTS}
	_log
}

jpda() {
	_log
	_log "JPDA variables set"
	export JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
	export JAVA_OPTS="${JAVA_OPTS} ${JPDA_OPTS}"
	_log JAVA_OPTS=${JAVA_OPTS}
	_log
}

start() {
	echo "Sapphire Home: ${SAPPHIRE_HOME}/sapphire-resources/${SAPPHIRE_CURRENT_VERSION}"
	echo "Java Home: ${JAVA_HOME}"
	echo ""
	for QUEUE in ${SAPPHIRE_RISK_QUEUE//,/ }; do
		local INSTANCE=${INSTANCE_PREFIX}_${QUEUE}
		local RUNTIME_PID=${SAPPHIRE_RUNTIME_DIR}/$INSTANCE.pid
		local PID=$(_get_pid $RUNTIME_PID)		
		_running $PID
		if [ "$PID" -ne 0 ] && [ $RUNNING  -ne 0 ];
		then
			echo "Server $INSTANCE is already running with PID $PID !!"
		else 
			echo "Server $INSTANCE starting it now ..."						
			_start $QUEUE			
		fi
		echo ""
	done
}

stop() {	
	for QUEUE in ${SAPPHIRE_RISK_QUEUE//,/ }; do
		local INSTANCE=${INSTANCE_PREFIX}_$QUEUE
		local RUNTIME_PID=${SAPPHIRE_RUNTIME_DIR}/$INSTANCE.pid
		local PID=$(_get_pid $RUNTIME_PID)
		_running $PID
		if [ "$PID" -ne 0 ] && [ $RUNNING -ne 0 ];
		then
			echo "Server $INSTANCE is running with PID $PID, stopping it now ..."
			_kill $PID
			echo "$INSTANCE is stopped"
		else 
			echo "Server $INSTANCE is not running !!"
		fi
		echo ""
	done
}

check() {
	echo "to do"
}

install() {	
	local PKG_VERSION=$1
	local RES_VERSION=$2
	if [ -z "$PKG_VERSION" ]; then
		echo "Wrong number of argument : please define a <package-version>"
		usage
	fi	
	if [ -z "$2" ]; then
		RES_VERSION=$PKG_VERSION
	fi	
	echo "$PKG_VERSION" "${RES_VERSION}"	
	local LOCK_FILE=install.lock
	if [ ! -f $LOCK_FILE ]
	then
		touch $LOCK_FILE
		_install_package $PKG_VERSION
		_install_resources $RES_VERSION
		rm -f $LOCK_FILE
	fi	
}

usage() {
	echo "Usage:"
	echo "$0 { start | start_jpda | stop | check | install <package-version> [<resources-version>] }"
	exit 1
}

_start() {
	local QUEUE=$1
	local INSTANCE=${INSTANCE_PREFIX}_$QUEUE
	local RUNTIME_PID=${SAPPHIRE_RUNTIME_DIR}/$INSTANCE.pid
	if [ ! -d "logs/${QUEUE}" ]; then
		mkdir -p logs/$QUEUE
	fi
	local DEBUG_LOC="logs/${QUEUE}/console.out"
	_log   
    _log
	SAP_OPTS="${JAVA_OPTS} -Dlog4j.configuration=${LOG4J_LOC}"
	SAP_OPTS="${SAP_OPTS} -DSAP_LOG4J_LOC_PREFIX=${SAP_LOG4J_LOC_PREFIX}/${QUEUE}/"
	SAP_OPTS="${SAP_OPTS} -Dinstance=$INSTANCE"
	SAP_OPTS="${SAP_OPTS} -DtopazServices=${QUEUE}"
	_log "$JAVA_HOME/bin/java -jar $SAP_OPTS ${LIBDIR}/sapphire-topaz-launcher.jar ${SAPPHIRE_LOC} > $DEBUG_LOC 2>&1 &"
	${JAVA_HOME}/bin/java -jar ${SAP_OPTS} "${LIBDIR}/sapphire-topaz-launcher.jar" "${SAPPHIRE_LOC}" > $DEBUG_LOC 2>&1 &
	if [ "$!" != "" ]; 
	then
		echo "$!" > $RUNTIME_PID
	else
		echo "${INSTANCE} failed to start"
	fi
}

_install_package() {
	local PKG_VERSION=$1
	if [ ! -d "sapphire-packages" ]; then
		echo "Creating sapphire-packages directory"
		mkdir sapphire-packages
	fi
	${MAVEN_HOME}/bin/mvn org.apache.maven.plugins:maven-dependency-plugin:2.5:get \
		-Dartifact=com.jpmorgan.sapphire:topazserver:$PKG_VERSION \
		-Dtransitive=false -Ddest=${SAP_SERVER_NAME}"-"$PKG_VERSION".zip" \
		-DremoteRepositories=local \
		-Dpackaging=zip	
	unzip ${SAP_SERVER_NAME}"-"$PKG_VERSION".zip" -d "sapphire-packages"
	chmod -R 755 "sapphire-packages/"$PKG_VERSION
	rm ${SAP_SERVER_NAME}"-"$PKG_VERSION".zip"
	rm -f sapphire-packages/${SAPPHIRE_CURRENT_VERSION}
	ln -sf $PKG_VERSION sapphire-packages/${SAPPHIRE_CURRENT_VERSION}
}

_install_resources() {
	if [ ! -d "sapphire-resources" ]; then
		echo "Creating sapphire-resources direcotry."
		mkdir sapphire-resources
	fi
	local RES_VERSION=$1
	${MAVEN_HOME}/bin/mvn org.apache.maven.plugins:maven-dependency-plugin:2.5:get \
		-Dartifact=com.jpmorgan.sapphire:com.jpmorgan.sapphire.resources:$RES_VERSION \
		-Dtransitive=false -Ddest=${SAP_SERVER_NAME}"-"$RES_VERSION".jar" \
		-DremoteRepositories=local \
		-Dpackaging=jar	
	unzip ${SAP_SERVER_NAME}"-"$RES_VERSION".jar" -d "sapphire-resources/"$RES_VERSION
	chmod -R 755 "sapphire-resources/"$RES_VERSION
	rm ${SAP_SERVER_NAME}"-"$RES_VERSION".jar"
	rm -f sapphire-resources/${SAPPHIRE_CURRENT_VERSION}
	ln -sf $RES_VERSION sapphire-resources/${SAPPHIRE_CURRENT_VERSION}
}

case "$1" in
	start)
		start
		;;
	start_jpda)
		jpda
		start
		;;
	stop)
		stop
		;;	
	check)
		check
		;;
	install)
		install $2 $3
		;;
	*)
		usage
		;;
esac